/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.br.imovel.converter;

import com.br.imovel.ejb.ProfessorEJBLocal;
import com.br.imovel.model.Professor;
import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author diego
 */
@FacesConverter(value = "professorConverter")
public class ProfessorConverter implements Converter {

    @EJB
    private ProfessorEJBLocal professorEJB;

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {

        if (value == null || value.equals("")) {
            return null;
        }
        Professor professor = new Professor();
        String[] valores = value.split("\\{\\$\\}");
        professor.setId(Long.valueOf(valores[0]));
        return professor;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
        Professor professor;
        if (value == null) {
            return null;
        }

        if (value.getClass().getName().equals("java.lang.Long")) {
            Long idProfessor = Long.parseLong(value.toString());
            return idProfessor.toString();
        }

        if (value instanceof Professor) {
            professor = (Professor) value;

        } else {
            throw new IllegalArgumentException("object:" + value + " of type:" + value.getClass().getName() + "; expected type: Professor");
        }

        return professor.getId() + "{$}";

    }
}
